package com.bjc.nio.zerocopy;

import com.sun.security.ntlm.Server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @program: nettypro
 * @description: 零拷贝
 * @author: bjc
 * @create: 2021-09-05 20:59
 */
public class NewIOServer {

    public static void main(String[] args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(7002);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(address);

        //创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        int readCount = 0;
        while (true){
            SocketChannel socketChannel = serverSocketChannel.accept();
            while (readCount!=-1){
                try {
                    readCount = socketChannel.read(byteBuffer);
                } catch (IOException e) {
                    e.printStackTrace();
                    break;
                }
                //倒带  position = 0；mark 作废
                byteBuffer.rewind();
            }
        }
    }
}
